<include file="Public/header"/>
<include file="Public/menu"/>
<style>
    .thumbimg{
        max-width: 50px;
        max-height: 50px;
    }
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            首页
            <small>采购</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('Manage/index/index')}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="{:U('vendor/index')}"><i class="fa fa-dashboard"></i> 采购</a></li>
            <li class="active">店铺可采购商品配置</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">

        <div class="tab-content">
            <div class="tab-pane active" id="tab_0">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title">{$local_name}</h3>
                        <div class="form-inline pull-right">
                            <form action="{:U('purchase/store_setup',array('local_id'=>$local_id,'order_field'=>'product_id','order'=>$order), '')}" method="get" pjax-container="">
                                <fieldset>
                                    <select class="form-control" name="search_field" style="height: 30px; font-size: 12px;">
                                        <option value="name" <if condition="$search_field eq 'name'">selected="selected"</if>>货品名称</option>
                                        <option value="barcode" <if condition="$search_field eq 'barcode'">selected="selected"</if>>条码</option>
                                        <option value="bn" <if condition="$search_field eq 'bn'">selected="selected"</if>>BN</option>
                                    </select>
                                    <div class="input-group input-group-sm">
                                        <div class="input-group input-group-sm">
                                            <input type="text" class="form-control" placeholder="" name="search" value="{$search}">
                                        </div>
                                    </div>

                                    <div class="input-group input-group-sm">
                                        <div class="input-group-btn">
                                            <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>
                                            </button>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
                        </div>

                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover">
                            <tbody>
                            <tr>
                                <th><input type="checkbox" class="grid-select-all"></th>
                                <th>货品ID<a class="fa fa-fw fa-sort" href="{:U('purchase/store_setup',array('local_id'=>$local_id, 'p'=>$p,'order_field'=>'product_id','order'=>$order), '')}"></a></th>
                                <th>商品ID<a class="fa fa-fw fa-sort" href="{:U('purchase/store_setup',array('local_id'=>$local_id, 'p'=>$p,'order_field'=>'goods_id','order'=>$order), '')}"></a></th>
                                <th>名称</th>
                                <th>图片</th>
                                <th>条码<a class="fa fa-fw fa-sort" href="{:U('purchase/store_setup',array('local_id'=>$local_id, 'p'=>$p,'order_field'=>'barcode','order'=>$order), '')}"></a></th>
                                <th>BN<a class="fa fa-fw fa-sort" href="{:U('purchase/store_setup',array('local_id'=>$local_id, 'p'=>$p,'order_field'=>'bn','order'=>$order), '')}"></a></th>
                                <th>指导价格<a class="fa fa-fw fa-sort" href="{:U('purchase/store_setup',array('local_id'=>$local_id, 'p'=>$p,'order_field'=>'guidance_price','order'=>$order), '')}"></a></th>
                                <th>最少采购个数</th>
                                <th>采购价格</th>
                            </tr>
                            <volist name="list" id="vo">
                                <tr class="cg-product-tr">
                                    <td><input type="checkbox" class="grid-item" data-id="{$vo.product_id}" <if condition="$vo.price neq ''">checked="checked"</if> ></td>
                                    <td>{$vo.product_id}</td>
                                    <td>{$vo.goods_id}</td>
                                    <td>{$vo.name}</td>
                                    <td><img src="{$vo.goods_id|getThumb=###}" data-bimg="{$vo.goods_id|getThumb=###}" alt="" class="scale-img"></td>
                                    <td>{$vo.barcode}</td>
                                    <td>{$vo.bn}</td>
                                    <td>￥{$vo.guidance_price}</td>
                                    <td>
                                        <input type="number" min="1" class="form-control" name="store" value="{$vo.store|default=500}">
                                    </td>
                                    <td>
                                        <input type="number" min="0.01" class="form-control" name="price" value="{$vo.price|default=$vo['guidance_price']}">
                                    </td>
                                </tr>
                            </volist>
                            </tbody>
                        </table>
                    </div>
                    <div class="box-footer clearfix">
                        <input type="checkbox" class="grid-select-all">&nbsp;&nbsp;&nbsp;
                        <a class="btn btn-sm btn-info batch-delete">保存</a>
                        <a class="btn btn-sm btn-primary grid-refresh"><i class="fa fa-refresh"></i></a>
                        {$page}
                    </div>
                    <!-- /.box-body -->
                </div>
            </div>

        </div>
    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>
<script type="text/javascript">
	showBigPicture();
</script>
<script data-exec-on-popstate>
    $(function () {

        $('.grid-select-all').change(function () {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });

        var ff = false;
        function controlMnm(i,j) {
            console.log(i, j);
            var shu = i.value;
            var min = i.min;
            if (shu < min) {
                alert('最少采购数量不能小于'+min+'个');
                ff = true;
            }
            var price = j.value;
            var min_price = j.min;
            if (price < min_price) {
                alert('采购价格不能小于'+min_price);
                ff = true;
            }
        }

        $('.batch-delete').on('click',function(e){

            if(!confirm("是否继续")){
                return false;
            }
            var data = [];
            $(".grid-item").each(function (i) {
                if ($(this)[0].checked) {
                    var is_show = true;
                    var input = $(this).parent().parent().find('input');
                    controlMnm(input[1], input[2]);

                } else {
                    var is_show = false;
                }

                var product_id = $(this).parent().parent().find('td:eq(1)').text();
                var goods_id = $(this).parent().parent().find('td:eq(2)').text();
                var store = $(this).parent().parent().find('input[name=store]').val();
                var price = $(this).parent().parent().find('input[name=price]').val();

                data.push({product_id:product_id,goods_id:goods_id,store:store,price:price,is_show:is_show});
            });
            if (ff) {
                return false;
            }
            if (data) {
                $.ajax({
                    type: "POST",
                    url: "{:U('purchase/save_store_setup',array('local_id'=>$local_id))}",
                    data: {data:data},
                    success: function(msg){
                        if (msg.status) {
                            alert(msg.message);
                        } else {
                            alert(msg.message)
                        }
                        window.location.reload();
                    },
                    error: function (msg) {
                        alert(msg.message);
                    }
                });
            }
        })

    });
</script>